# -*- coding: utf-8 -*-

import time
from report import report_sxw
import pooler
from osv import osv
from tools.translate import _
import random
from datetime import datetime
import logging
        
class Parser(report_sxw.rml_parse):
    logging.getLoggerClass()
        
    def __init__(self, cr, uid, name, context):
        
        super(Parser, self).__init__(cr, uid, name, context=context)
        pool = pooler.get_pool(cr.dbname)
        self.dshb_obj = pool.get('ctsv.danhsachhocbong')
        self.hbs_obj = pool.get('ctsv.getdshb')
        self.semester_obj = pool.get('aas.academic.semester')
        
        self.localcontext.update({
            'init': self._init,
            'lines': self.get_lines,
            'semester': self.get_semester,
            'total_scholarship': self.get_total,
            'get_logo': self.get_logo,
        })
    
    def _init(self, dshocbong):
        logging.info('----DSHOCBONG ID : %s', dshocbong.id)
        self.lines = self.dshb_obj.get_all_lines(self.cr, self.uid, dshocbong.id)
        logging.info('----LINE SIZE: %s', len(self.lines))
        hb = self.hbs_obj.browse(self.cr, self.uid, dshocbong.id, context=False)
        self.semester = str(hb.semester.name) + " (" + str(hb.semester.academic_year.name) + ")"
        logging.info('----REPORT SEMESTER: %s', self.semester)
        
#        calculate total scholarship
        self.total = 0
        for line in self.lines:
            logging.info('----line[5]: %s',line[5])
            self.total += line[5]
        logging.info('----Total schor: %s',self.total)
        return True

    def get_lines(self):
        return self.lines
    
    def get_semester(self):
        return self.semester

    def get_total(self):
        return self.total
    
    
    def get_logo(self):
        user = self.pool.get('res.users').browse(self.cr, self.uid, self.uid)
        logo = False
        if user.company_id:
            company = self.pool.get('res.company').browse(self.cr, self.uid, user.company_id.id)
            logo = company.logo
        return logo
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
